<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    //商品列表
    public function getIndex()
    {
       
        $goods = DB::table('goods')->get();
        return view('admin.goods.index',['goods'=>$goods]);
    }
    //商品添加
    public function getAdd()
    {
        $cates = self::CateList(); 
    	return view('admin.goods.add',['cate'=>$cates]);
    } 

    static public function CateList()
    {
       $cates = DB::select('select *,concat(path,",",id) as paths from cate order by paths');
        //dd($cates);
        foreach ($cates as $k => $v) {
            $arr = explode(',',$v->path);
            $len = count($arr)-1;
            $cates[$k]->name = str_repeat('|--',$len).$v->name;
        }
           return $cates;
    }

    //执行添加
    public function postInsert(Request $request)
    {
        //dd($request->all());
         if($request->hasFile('pic')){
               //随机文件名
                  $name = md5(time()+rand(1,999));
               //获取后缀名
                  $su = $request->file('pic')->getClientOriginalExtension();
                  // dd($su);jpg png jpeg gif tiff
                  $arr = array('jpg','png','jpeg','tiff');
               //判断
                  if(!in_array($su,$arr)){
                  // die('非法上传');
                  echo '<script type="text/javascript">
                        alert("上传文件不合法");
                        location.href="http://niuwan.com/admin/goods/add";
                        </script>';
                  }
               //将文件移动到指定目录下
               $request->file('pic')->move('./goods/', $name.'.'.$su);
               //echo '<img src="/uploads/'.$name.'.jpg" alt="" width="50" height="50">';
              $data['pic'] =  $name.'.'.$su;
            }
        $data = $request->only('title','pic','price','url','cid');
        $res = DB::table('goods')->insert($data);
         //判断是否插入成功
            if($res){
                  return redirect('/admin/goods/index')->with('success','商品添加成功');
            }else{
               return back()->with('error','商品添加失败');
            }
    }

     //修改
      public function getEdit($id)
      {
         //echo $id;
         //根据商品id查询
         $goods = DB::table('goods')->where('id',$id)->first();
         //解析模板
         return view('admin.goods.edit',['goods'=>$goods]);
      }

      //修改
      public function postUpdate(Request $request)
      {
         $id = $request->only(['id']);
         //echo $id;
         $data = $request->only(['title','pic','price','url','cid']);
         //dd($data);
         //执行修改
         $res = DB::table('goods')->where('id',$id)->update($data);
         //dd($res);
          //判断是否修改成功
            if($res){
               return redirect('/admin/goods/index')->with('success','商品修改成功');
            }else{
               return back()->with('error','商品修改失败');
            }
      }

      //删除
       public function getDelete($id)
       {
         //dd($id);
         //执行
         //$user = DB::table('user')->where('id',$id)->first();

         $res = DB::table('goods')->where('id',$id)->delete();
         //dd($res);
         if($res){
               return redirect('/admin/goods/index')->with('success','用户删除成功');
            }else{
               return back()->with('error','用户删除失败');
            }
       }
}
